package com.mymoney.resources;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mymoney.auth.DeviceContext;
import com.mymoney.beans.DeviceBean;
import com.mymoney.beans.ItemBean;
import com.mymoney.exceptions.MyMoneyException;
import com.mymoney.persistence.PersistenceContext;
import com.mymoney.persistence.TransactionalAction;
import com.mymoney.persistence.managers.ItemManager;
import com.mymoney.utils.ServiceUtils;

@Path("/items")
public class ItemResource extends AbstractWebResource {
	
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public Response list() {
		Response response = null;
		PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		
		try
		{
			if(persistenceContext != null)
			{
				ItemManager itemManager = persistenceContext.getItemManager();
				List<ItemBean> itemList = itemManager.list();
				response = Response.ok(itemList).type(MediaType.APPLICATION_JSON).build();
			}
		}catch (Exception e) {
			response = ServiceUtils.getErrorResponse(111, e);
		}
		
		return response;
	}
	
	@GET
	@Path("/planned")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response listPlanned() {
		Response response = null;
		PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		
		try
		{
			if(persistenceContext != null)
			{
				DeviceContext deviceContext = getDeviceContext();
				final DeviceBean device = deviceContext.getDevice();
				if(device != null)
				{
					ItemManager itemManager = persistenceContext.getItemManager();
					String primaryAccount = device.getPrimaryAccount();
					List<ItemBean> itemList = itemManager.listPlanned(primaryAccount);
					response = Response.ok(itemList).type(MediaType.APPLICATION_JSON).build();
				}
				else
				{
					response = Response.noContent().type(MediaType.APPLICATION_JSON).build();
				}
			}
		}catch (Exception e) {
			response = ServiceUtils.getErrorResponse(111, e);
		}
		
		return response;
	}
	
	@GET
	@Path("/bought")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response listBought() {
		Response response = null;
		PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		
		try
		{
			if(persistenceContext != null)
			{
				DeviceContext deviceContext = getDeviceContext();
				final DeviceBean device = deviceContext.getDevice();
				if(device != null)
				{
					ItemManager itemManager = persistenceContext.getItemManager();
					String primaryAccount = device.getPrimaryAccount();
					List<ItemBean> itemList = itemManager.listBought(primaryAccount);
					response = Response.ok(itemList).type(MediaType.APPLICATION_JSON).build();
				}
				else
				{
					response = Response.noContent().type(MediaType.APPLICATION_JSON).build();
				}
			}
		}catch (Exception e) {
			response = ServiceUtils.getErrorResponse(111, e);
		}
		
		return response;
	}
	
	
	@POST
	@Path("/item")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response add(final ItemBean itemBean) {

		final PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		try {

			TransactionalAction action = new TransactionalAction() {

				public void run() throws MyMoneyException {
					ItemManager itemManager = persistenceContext.getItemManager();
					itemManager.add(itemBean);
				};

			};
			persistenceContext.run(action);
			return Response.ok("EVALA").type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(111, e);
			return response;
		}
	}
	
	@GET
	@Path("/item/{code}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getById(final @PathParam("code") long code) {

		PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		try {
			ItemManager itemManager = persistenceContext.getItemManager();
			ItemBean itemBean = itemManager.getByCode(code);

			return Response.ok(itemBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(111, e);
			return response;
		}
	}
	
	@PUT
	@Path("/item/{code}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response update(final @PathParam("code") long code,
			final ItemBean itemBean) {

		final PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		try {

			TransactionalAction action = new TransactionalAction() {

				public void run() throws MyMoneyException {
					ItemManager itemManager = persistenceContext.getItemManager();
					itemManager.update(itemBean, code);
				};

			};
			persistenceContext.run(action);

			return Response.ok("EVALA").type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(111, e);
			return response;
		}
	}
}
